package com.joolun.activity.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joolun.activity.domain.ActivityAwardInfo;
import com.joolun.activity.domain.ActivityInfo;
import com.joolun.activity.domain.ActivityTakeInfo;
import com.joolun.activity.mapper.ActivityTakeInfoMapper;
import com.joolun.activity.service.IActivityAwardInfoService;
import com.joolun.activity.service.IActivityInfoService;
import com.joolun.activity.service.IActivityTakeInfoService;
import com.joolun.activity.vo.ActivityTakeVo;
import com.joolun.activity.vo.ShareInfoVo;
import com.joolun.common.core.domain.entity.SysUser;
import com.joolun.common.utils.DateUtils;
import com.joolun.common.utils.GeneratorUtils;
import com.joolun.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.joolun.activity.mapper.ActivityTakeMapper;
import com.joolun.activity.domain.ActivityTake;
import com.joolun.activity.service.IActivityTakeService;

/**
 * 活动参与码Service业务层处理
 *
 * @author www.joolun.com
 * @date 2022-11-22
 */
@Service
public class ActivityTakeServiceImpl extends ServiceImpl<ActivityTakeMapper, ActivityTake> implements IActivityTakeService
{
    @Autowired
    private ActivityTakeMapper activityTakeMapper;
    @Autowired
    private IActivityTakeInfoService iActivityTakeInfoService;
    @Autowired
    private IActivityInfoService iActivityInfoService;
    /**
     * 查询活动参与码
     *
     * @param id 活动参与码ID
     * @return 活动参与码
     */
    @Override
    public ActivityTake selectActivityTakeById(String id)
    {
        return activityTakeMapper.selectActivityTakeById(id);
    }

    /**
     * 查询活动参与码列表
     *
     * @param activityTake 活动参与码
     * @return 活动参与码
     */
    @Override
    public List<ActivityTake> selectActivityTakeList(ActivityTake activityTake)
    {
        return activityTakeMapper.selectActivityTakeList(activityTake);
    }

    /**
     * 新增活动参与码
     *
     * @param activityTakeVo 活动参与码
     * @return 结果
     */
    @Override
    public String insertActivityTakeVo(ActivityTakeVo activityTakeVo)
    {
        ActivityTake activityTake=new ActivityTake();
        activityTake.setTakeNum(activityTakeVo.getTakeNum());
        activityTake.setActivityCode(activityTakeVo.getActivityCode());
        if(activityTake.getTakeNum()<=0){
            throw new RuntimeException("活动参与码数量小于1");
        }
        if(activityTake.getTakeNum()>=101){
            throw new RuntimeException("活动参与码数量大于100");
        }
        //判断活动是否开始
        ActivityInfo activityInfo= iActivityInfoService.getOne(new QueryWrapper<ActivityInfo>()
                .eq("activity_code",activityTake.getActivityCode()));
        if(!"2".equals(activityInfo.getStatus())){
            throw new RuntimeException("活动还未开始");
        }
        activityTake.setTakeCode(GeneratorUtils.getSecurityCode(8, GeneratorUtils.SecurityCodeLevel.Hard,false).toUpperCase());
        activityTake.setCreateTime(DateUtils.getNowDate());
        SysUser sysUser= SecurityUtils.getLoginUser().getUser();
        activityTake.setUserId(sysUser.getUserId());
        activityTake.setCheckUser(sysUser.getUserName());
        activityTake.setDeptId(sysUser.getDept().getDeptId());
        activityTake.setDeptName(sysUser.getDept().getDeptName());
        List<ActivityTakeInfo> activityTakeInfos=new ArrayList<>();
        for(int i=0;i<activityTake.getTakeNum();i++){
            ActivityTakeInfo activityTakeInfo=new ActivityTakeInfo();
            activityTakeInfo.setActivityCode(activityTake.getActivityCode());
            activityTakeInfo.setTakeCode(activityTake.getTakeCode());
            activityTakeInfo.setActivityTakeCode(activityTake.getTakeCode()+GeneratorUtils.getSecurityCode(4, GeneratorUtils.SecurityCodeLevel.Hard,false).toUpperCase());
            activityTakeInfo.setStatus("1");
            activityTakeInfo.setCreateTime(DateUtils.getNowDate());
            activityTakeInfo.setGenUserId(activityTake.getUserId());
            activityTakeInfo.setGenUser(activityTake.getCheckUser());
            activityTakeInfo.setGenDeptId(activityTake.getDeptId());
            activityTakeInfo.setGenDept(activityTake.getDeptName());
            activityTakeInfos.add(activityTakeInfo);
        }
        iActivityTakeInfoService.saveBatch(activityTakeInfos);
        save(activityTake);
        return activityTake.getTakeCode();
    }

    /**
     * 新增活动参与码
     *
     * @param activityTake 活动参与码
     * @return 结果
     */
    @Override
    public String insertActivityTake(ActivityTake activityTake)
    {
        if(activityTake.getTakeNum()<=0){
            throw new RuntimeException("活动参与码数量小于1");
        }
        //判断活动是否开始
        ActivityInfo activityInfo= iActivityInfoService.getOne(new QueryWrapper<ActivityInfo>()
                .eq("activity_code",activityTake.getActivityCode()));
        if(!"2".equals(activityInfo.getStatus())){
            throw new RuntimeException("活动还未开始");
        }
        activityTake.setTakeCode(GeneratorUtils.getSecurityCode(6, GeneratorUtils.SecurityCodeLevel.Hard,false).toUpperCase());
        activityTake.setCreateTime(DateUtils.getNowDate());
        SysUser sysUser= SecurityUtils.getLoginUser().getUser();
        activityTake.setUserId(sysUser.getUserId());
        activityTake.setCheckUser(sysUser.getUserName());
        activityTake.setDeptId(sysUser.getDept().getDeptId());
        activityTake.setDeptName(sysUser.getDept().getDeptName());
        List<ActivityTakeInfo> activityTakeInfos=new ArrayList<>();
        for(int i=0;i<activityTake.getTakeNum();i++){
            ActivityTakeInfo activityTakeInfo=new ActivityTakeInfo();
            activityTakeInfo.setActivityCode(activityTake.getActivityCode());
            activityTakeInfo.setTakeCode(activityTake.getTakeCode());
            activityTakeInfo.setActivityTakeCode(activityTake.getTakeCode()+GeneratorUtils.getSecurityCode(2, GeneratorUtils.SecurityCodeLevel.Hard,false).toUpperCase());
            activityTakeInfo.setStatus("1");
            activityTakeInfo.setCreateTime(DateUtils.getNowDate());
            activityTakeInfo.setGenUser(activityTake.getCheckUser());
            activityTakeInfo.setGenUserId(activityTake.getUserId());
            activityTakeInfo.setGenDept(activityTake.getDeptName());
            activityTakeInfo.setGenDeptId(activityTake.getDeptId());
            activityTakeInfos.add(activityTakeInfo);
        }
        iActivityTakeInfoService.saveBatch(activityTakeInfos);
        save(activityTake);
        return activityTake.getTakeCode();
    }

    /**
     * 修改活动参与码
     *
     * @param activityTake 活动参与码
     * @return 结果
     */
    @Override
    public int updateActivityTake(ActivityTake activityTake)
    {
        return activityTakeMapper.updateActivityTake(activityTake);
    }

    /**
     * 批量删除活动参与码
     *
     * @param ids 需要删除的活动参与码ID
     * @return 结果
     */
    @Override
    public int deleteActivityTakeByIds(String[] ids)
    {
        return activityTakeMapper.deleteActivityTakeByIds(ids);
    }

    /**
     * 删除活动参与码信息
     *
     * @param id 活动参与码ID
     * @return 结果
     */
    @Override
    public int deleteActivityTakeById(String id)
    {
        return activityTakeMapper.deleteActivityTakeById(id);
    }

}
